﻿var BackUrl = "";
var Total = 0;


///增加数量；
function AddCartNum(bianhao) {
    var quantity = parseInt($("#txtQuantity" + bianhao).val(), 10) + 1;
    $("#txtQuantity" + bianhao).val(quantity);
    ModifyQuantity(bianhao);
}

///减少数量；
function SubCartNum(bianhao) {
    var quantity = parseInt($("#txtQuantity" + bianhao).val(), 10);
    if (quantity > 1)
        quantity--;
    $("#txtQuantity" + bianhao).val(quantity);
    ModifyQuantity(bianhao);
}

//调整服务器端购物车数量；并更新本机客户端数据；
function ModifyQuantity(bianhao) {
    UpdateCartCounts();
    //更新服务器端数据；
    var url = "/home/as/shoppingcartcommon.aspx?Action=ModifyQuantity&ShoppingCartID=" + $("#hfCartID" + bianhao).val();
    url += "&Quantity=" + $("#txtQuantity" + bianhao).val();
    AjaxGetEx(url, "", function (ret) {
        if (ret.Result == 1) {
        }
        else {
            alert("提示：" + ret.Message);
        }
    }, true);

}

///更新购物车统计
function UpdateCartCounts() {
    var totalQuantity = 0;
    var totalMoney = 0.00;
    for (var i = 0; i < Total; ++i) {
        var quantity = parseInt($("#txtQuantity" + i).val(), 10);
        var price = parseFloat($("#hfPrice" + i).val(), 10);
        totalQuantity += quantity;
        totalMoney = totalMoney.Add(price.Mul(quantity));
    }
    $("#spTotalQuantity").html(totalQuantity);
    $("#spTotalMoney").html(formatnumber(totalMoney, 2));

    $("#spShoppingCartCounts").html(totalQuantity);
    $("#spShoppingCartsMoneys").html(formatnumber(totalMoney, 2));
}







//删除购物车中指定的商品；
function DeleteCart(id) {
    if (!confirm("提示：您确定要从购物车移除该商品吗？"))
        return;
    var url = "/home/as/shoppingcartcommon.aspx?Action=DeleteCart&ShoppingCartID=" + id;
    AjaxGetEx(url, "", function (ret) {
        if (ret.Result == 1) {
            location.reload();
        }
        else {
            alert("提示：" + ret.Message);
        }
    }, true);
}

///清空购物车；
function ClearCarts() {
    if (!confirm("提示：您确定要清空购物车的所有商品？"))
        return;
    var url = "/home/as/shoppingcartcommon.aspx?Action=DeleteAllShopping" ;
    AjaxGetEx(url, "", function (ret) {
        if (ret.Result == 1) {
            location.reload();
        }
        else {
            alert("提示：" + ret.Message);
        }
    }, true);

}

//继续购物
function GoToBuy() {
    if (BackUrl != "")
        location.href = BackUrl;
    else
        location.href = "/index.aspx";
}




